package com.lerays.spider.sougou;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.impl.client.CloseableHttpClient;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.lionsoul.websnail.Page;
import org.lionsoul.websnail.Spider;
import org.lionsoul.websnail.analyzer.Analyzer;
import org.xml.sax.InputSource;

import com.lerays.spider.sougou.model.SourceBean;
import com.lerays.spider.sougou.model.StreamBean;

/**
 * wechat history page analyzer
 * 
 * after 2015.06.18 cuz wechat changed the access rule
 * 
 * @author chenxin<chenxin6193152@gmail.com>
 */
public class SougouAnalyzer implements Analyzer {
        SougouSpider sougouSpider = null;
        private CloseableHttpClient httpClient = null;
        public SougouAnalyzer(SougouSpider sougouSpider) {
                this.sougouSpider = sougouSpider;
        }

        @Override
        public void process(Spider spider, Page page) {
                String u = page.getUrl();
                String part = u.split("gzh")[1];                
                URL postUrl;
                String con = "";
                try {
                        postUrl = new URL("http://weixin.sogou.com/gzhjs"+part);
                        // 打开连接 
                        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); 
                        //打开读写属性，默认均为false                 
                        connection.setDoOutput(true);  
                        connection.setDoInput(true); 
                        connection.setUseCaches(false); 
                        connection.setInstanceFollowRedirects(true); 
                        // 配置连接的Content-type，配置为application/x- www-form-urlencoded的意思是正文是urlencoded编码过的form参数，下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码 
                        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2");
                        
                        connection.setRequestProperty("Connection", "        keep-alive");
                        
                        connection.setRequestProperty("Cookie", "CXID=2B88E7A4C81D54425EFD6E0F1EB52B25; SUID=63A211DA496C860A565D209300064DD2; SUV=003A79B8DA11A26356811BABAC8BF370; ABTEST=0|1452769886|v1; weixinIndexVisited=1; IPLOC=CN4403; ld=Kkllllllll2QXPGtlllllVzW9eZlllllLPpAYZllll9lllll9holl5@@@@@@@@@@; Hm_lvt_1b78f52870e292db0612b72b3837ffd0=1453697413; ad=Elllllllll2QgvjFlllllVzXUiklllllLPpAYZllllGlllllpOxlw@@@@@@@@@@@; SNUID=07C172B963614E388FFB6C016414874D; sct=42");

                        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
                        String line;

                        while ((line = reader.readLine()) != null) { 
                                con += line;
                        } 
                        reader.close(); 
                } catch (IOException e1) {
                        e1.printStackTrace();
                }         
                //System.out.println(con);
                // return the json analyzer of the target source
                JSONObject json = JSONObject.fromObject(con);
                String items = json.getString("items");
                //System.out.println(items);
                JSONArray jarr = JSONArray.fromObject(items);
                //System.out.println(jarr.get(0));
                for (int i = 0; i < jarr.size(); i++) {
//                        try {
//                                Thread.sleep(3000);
//                        } catch (InterruptedException e2) {
//                                // TODO Auto-generated catch block
//                                e2.printStackTrace();
//                        }
                        String str = jarr.getString(i);

                        Date nowTime=new Date();
                        SimpleDateFormat time=new SimpleDateFormat("yyyy-M-dd"); 
                        String today = time.format(nowTime);
                        //System.out.println(time.format(nowTime)); 
                        //if (str.contains(today)) {              //只抓当天
                                Pattern pattern = Pattern.compile("/websearch.*?]]>");
                                Matcher matcher = pattern.matcher(str);
                                String link = "";
                                if (matcher.find() == true) {
                                        link = "http://weixin.sogou.com"+matcher.group(0).replace("]]>", "");
                                        //System.out.println(link);    
                                        String htm = "";
                                        try {
                                                postUrl = new URL(link);
                                                // 打开连接 
                                                HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); 
                                                //打开读写属性，默认均为false                 
                                                connection.setDoOutput(true);  
                                                connection.setDoInput(true); 
                                                connection.setUseCaches(false); 
                                                connection.setInstanceFollowRedirects(true); 
                                                // 配置连接的Content-type，配置为application/x- www-form-urlencoded的意思是正文是urlencoded编码过的form参数，下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码 
                                                connection.setRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2");                                                
                                                connection.setRequestProperty("Connection", "keep-alive");                                                
                                                connection.setRequestProperty("Cookie", "CXID=2B88E7A4C81D54425EFD6E0F1EB52B25; SUID=63A211DA496C860A565D209300064DD2; SUV=003A79B8DA11A26356811BABAC8BF370; ABTEST=0|1452769886|v1; weixinIndexVisited=1; IPLOC=CN4403; ld=Kkllllllll2QXPGtlllllVzW9eZlllllLPpAYZllll9lllll9holl5@@@@@@@@@@; Hm_lvt_1b78f52870e292db0612b72b3837ffd0=1453697413; ad=Elllllllll2QgvjFlllllVzXUiklllllLPpAYZllllGlllllpOxlw@@@@@@@@@@@; SNUID=07C172B963614E388FFB6C016414874D; sct=42");
                                                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
                                                String line;

                                                while ((line = reader.readLine()) != null) { 
                                                        htm += line;
                                                        //System.out.println(line);                                                         
                                                } 
                                                reader.close(); 
                                        } catch (IOException e1) {
                                                // TODO Auto-generated catch block
                                                e1.printStackTrace();
                                        }  
                                        //JSONObject json = new JSONObject(htm);
                                        Pattern p = Pattern.compile("var msg_title.*?;");
                                        Matcher m = p.matcher(htm);
                                        String title = "";
                                        if (m.find() == true) {
                                                title = m.group(0).replace("\"", "").replace(";", "").split("=")[1];
                                                System.out.println(title); 
                                        }
                                        
                                        p = Pattern.compile("var msg_link.*?\";");
                                        m = p.matcher(htm);
                                        String srcLink = "";
                                        if (m.find() == true) {
                                                srcLink = "http://"+m.group(0).replace("\";", "").replace("\"", "").split("//")[1];
                                                System.out.println(srcLink); 
                                        }
                                        
                                        p = Pattern.compile("var msg_cdn_url.*?;");
                                        m = p.matcher(htm);
                                        String imgSrc = "";
                                        if (m.find() == true) {
                                                imgSrc = m.group(0).replace("\"", "").replace(";", "").split("://")[1];
                                                System.out.println(imgSrc); 
                                        }
                                        
                                        p = Pattern.compile("var ct.*?;");
                                        m = p.matcher(htm);
                                        String datetime = "";
                                        if (m.find() == true) {
                                                datetime = m.group(0).replace("\"", "").replace(";", "").split("=")[1];
                                                System.out.println(datetime); 
                                        }
                                        
                                        ArrayList<StreamBean> records = new ArrayList<StreamBean>();
                                        records.add(StreamBean
                                                      .create()
                                                      .setCateId(0)
                                                      .setSrcId(0)
                                                      .setTitle(title)
                                                      .setImgSrc(imgSrc)
                                                      .setSrcLink(srcLink)
                                                      .setPubtime(Integer.parseInt(datetime.trim()))
                                                      .setScores(0)
                                                      .setReadNum(0));
                                        
                                        // add the record
                                        if (records.size() > 0) {
                                                try {
                                                         sougouSpider.getDao().streamBatchInsert(
                                                                        records);
                                                } catch (SQLException e) {
                                                        sougouSpider.increaseFailedTaskCount();
                                                        e.printStackTrace();
                                                }
                                        }
//                                        records.clear();
//                                        records = null; // let gc do its work
                                }      
                      //  }                        
                }      
        }
}
